package com.base.caselib.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.base.caselib.R;
import com.base.caselib.bean.CaseBean;
import com.base.caselib.bean.CheckBoxBean;
import com.base.caselib.bean.DropBean;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.SingleChooseDialog;
import com.base.commonlib.widget.SingleSearchDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weikailiang on 2019/10/14.
 */

public class SpinnerCustom extends FrameLayout implements View.OnClickListener{

    private CaseBean.ModulesBean.RowsBean row = null;
    private String mLastModelIndex = "";
    private TextView mName;
    private TextView mTitle;
    public List<CheckBoxBean> mList = new ArrayList<>();
    private Map<String,String> mMap = new HashMap<>();
    private OnCheckFuOnClick listener;
    private String mCurrentModelIndex = "";
    private String mRowIndex;
    private CheckBoxBean mCurrentBoxBean;
    private String lastIndext;
    private String mRoadId;
    private boolean isDefault;
    public SpinnerCustom(@NonNull Context context) {
        super(context);
        init(null);
    }

    public SpinnerCustom(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public SpinnerCustom(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    private void init(AttributeSet attrs){
        LayoutInflater.from(getContext()).inflate(R.layout.item_spinner_single, this);
        mName = findViewById(R.id.tv_name);
        mTitle = findViewById(R.id.tv_text);
        mTitle.setOnClickListener(this);

    }
    public void setmName(String name){
        if (mName != null){
            mName.setText(name);
        }
    }

    public boolean isDefault() {
        return isDefault;
    }

    public void setDefault(boolean aDefault) {
        isDefault = aDefault;
    }

    public String getmRoadId() {
        return mRoadId;
    }

    public void setmRoadId(String mRoadId) {
        this.mRoadId = mRoadId;
    }

    public String getLastIndext() {
        return lastIndext;
    }

    public void setLastIndext(String lastIndext) {
        this.lastIndext = lastIndext;
    }

    public String getName(){
        return mName.getText().toString().trim();
    }
    public String getFileSave(){
        return row.getFieldSave();
    }
    public void setHintName(String name){
        if (mTitle != null){
            mTitle.setHint(name);
        }
    }
    public void setmTitle(String title){
        if (mTitle != null){
            mTitle.setText(title);
        }
    }
    public void setDefault(CheckBoxBean bean){
        setmTitle(bean.getDropLabel());
        mCurrentBoxBean = bean;
        mLastModelIndex = mMap.get(bean.getDropValue());
    }


    public void setData(List<CheckBoxBean> list){
        this.mList.clear();
        this.mList.addAll(list);
        if (mCurrentBoxBean!=null){
            for (CheckBoxBean bean : mList){
                if (bean.getDropLabel().equals(mCurrentBoxBean.getDropLabel())){
                    mTitle.setText(mCurrentBoxBean.getDropLabel());
                }
            }
        }
    }
    public void setRow(CaseBean.ModulesBean.RowsBean row){
        this.row = row;
    }
    public String getCurrentValue(){
        if (mList != null && mList.size()>0){
            for (CheckBoxBean bean : mList){
                if (bean.getDropLabel().equals(mCurrentBoxBean.getDropLabel())){
                    return bean.getDropValue();
                }
            }
        }else {
//            ToastUtil.show("未找到数据");
        }
        return null;
    }

    public void setCurrentModelIndex(String currentModelIndex){
        this.mCurrentModelIndex = currentModelIndex;
    }
    public String getCurrentModelIndex(){
        return mCurrentModelIndex;
    }

    public String getRowIndex(){
        return row.getRowIndex()+"";
    }

    public void show(boolean isFirstCreate){

        if (mList == null || mList.size()==0){
            if (row != null){
                if (listener != null){
                    listener.onRequestList(null,mCurrentModelIndex,isFirstCreate);
                }else {
                    ToastUtil.show("请退出该界面重新渲染！");
                }
            }else {
                ToastUtil.show("请退出该界面重新渲染");
            }
        }else {
            if (isFirstCreate){
                if (mList!=null&&mList.size()>0) {
                    if (isDefault){
                        isDefault = false;
                        if (mCurrentBoxBean!=null){
                            boolean flag = true;
                            for (CheckBoxBean bean : mList){
                                if (bean.getDropLabel().equals(mCurrentBoxBean.getDropLabel())){
                                    flag = false;
                                    mTitle.setText(mCurrentBoxBean.getDropLabel());
                                }
                            }
                            if (flag){
                                CheckBoxBean bean = (CheckBoxBean) mList.get(0);
                                mCurrentBoxBean = bean;
                                mTitle.setText(bean.getDropLabel());
                            }
                        }else {
                            CheckBoxBean bean = (CheckBoxBean) mList.get(0);
                            mCurrentBoxBean = bean;
                            mTitle.setText(bean.getDropLabel());
                        }
                    }else {
                        CheckBoxBean bean = (CheckBoxBean) mList.get(0);
                        mCurrentBoxBean = bean;
                        mTitle.setText(bean.getDropLabel());
                    }
                }else {
                    if (mCurrentBoxBean !=null){
                        mTitle.setText(mCurrentBoxBean.getDropLabel());
                    }
                }
            }else {
                SingleSearchDialog<CheckBoxBean> dialog = new SingleSearchDialog<CheckBoxBean>(getContext(), mName.getText().toString().toString(), mList, new SingleSearchDialog.OnItemChooseListener() {
                    @Override
                    public void onItemChoose(Object o) {

                        CheckBoxBean bean = (CheckBoxBean) o;
                        mCurrentBoxBean = bean;
                        //显示模块
                        listener.onCheckedListener(bean, mMap.get(bean.getDropValue()), mLastModelIndex);
                        //隐藏上次选择模块
                        listener.onUnCheckListener(bean, mLastModelIndex);
                        mTitle.setText(bean.getDropLabel());
                        //记录这次的索引
                        mLastModelIndex = mMap.get(bean.getDropValue());
                    }
                });
                dialog.show();
            }
        }
    }
    public void show(boolean isFirstCreate,boolean isShow){

        if (mList == null || mList.size()==0){
            if (row != null){
                if (listener != null){
                    listener.onRequestList(null,mCurrentModelIndex,isFirstCreate);
                }else {
                    ToastUtil.show("请退出该界面重新渲染！");
                }
            }else {
                ToastUtil.show("请退出该界面重新渲染");
            }
        }else {
            if (isShow) {
                SingleSearchDialog<CheckBoxBean> dialog = new SingleSearchDialog<CheckBoxBean>(getContext(), mName.getText().toString().toString(), mList, new SingleSearchDialog.OnItemChooseListener() {
                    @Override
                    public void onItemChoose(Object o) {

                        CheckBoxBean bean = (CheckBoxBean) o;
                        mCurrentBoxBean = bean;
                        //显示模块
                        listener.onCheckedListener(bean, mMap.get(bean.getDropValue()), mLastModelIndex);
                        //隐藏上次选择模块
                        listener.onUnCheckListener(bean, mLastModelIndex);
                        mTitle.setText(bean.getDropLabel());
                        //记录这次的索引
                        mLastModelIndex = mMap.get(bean.getDropValue());
                    }
                });
                dialog.show();
            }else {
                if (mList!=null&&mList.size()>0) {
                    CheckBoxBean bean = (CheckBoxBean) mList.get(0);
                    mCurrentBoxBean = bean;
                    mTitle.setText(bean.getDropLabel());
                }else {
                    if (mCurrentBoxBean !=null){
                        mTitle.setText(mCurrentBoxBean.getDropLabel());
                    }
                }
//                CheckBoxBean bean = (CheckBoxBean) mList.get(0);
//                mCurrentBoxBean = bean;
//                //显示模块
//                listener.onCheckedListener(bean, mMap.get(bean.getDropValue()), mLastModelIndex);
//                //隐藏上次选择模块
//                listener.onUnCheckListener(bean, mLastModelIndex);
//                mTitle.setText(bean.getDropLabel());
//                //记录这次的索引
//                mLastModelIndex = mMap.get(bean.getDropValue());
            }
        }

    }

    public CheckBoxBean getmCurrentBoxBean() {
        if (mCurrentBoxBean == null){
            return new CheckBoxBean();
        }
        return mCurrentBoxBean;
    }

    public String getmRowIndex() {
        return mRowIndex;
    }

    public void setmRowIndex(String mRowIndex) {
        this.mRowIndex = mRowIndex;
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_text) {
            show(false);
        }
    }

    public void setMapKeyValue(Map<String,String> map){
        if (map != null&&map.size()>0){
            mMap = map;
        }
    }
    public void notifyDataCase(){
        if (mList!=null&&mList.size()>0) {
            if (mCurrentBoxBean!=null){
                boolean flag = true;
                for (CheckBoxBean bean : mList){
                    if (bean.getDropLabel().equals(mCurrentBoxBean.getDropLabel())){
                        flag = false;
                        mTitle.setText(mCurrentBoxBean.getDropLabel());
                    }
                }
                if (flag){
                    CheckBoxBean bean = (CheckBoxBean) mList.get(0);
                    mCurrentBoxBean = bean;
                    mTitle.setText(bean.getDropLabel());
                }
            }else {
                CheckBoxBean bean = (CheckBoxBean) mList.get(0);
                mCurrentBoxBean = bean;
                mTitle.setText(bean.getDropLabel());
            }
        }else {
            if (mCurrentBoxBean !=null){
                mTitle.setText(mCurrentBoxBean.getDropLabel());
            }
        }
    }

    public void setOnCheckListener(OnCheckFuOnClick listener){
        this.listener = listener;
    }

    public interface OnCheckFuOnClick{
        void onCheckedListener(CheckBoxBean bean, String modeIndex,String lastModexInsex);
        void onUnCheckListener(CheckBoxBean bean,String modeIndex);
        void onRequestList(CheckBoxBean bean, String modeIndex,boolean isFirstCreate);
    }

}
